<template>
  <div>
    <el-row>
      <el-table
        :data="list.data"
        stripe
        border
        size="mini"
      >
        <el-table-column type="index" width="55"/>
        <el-table-column
          prop="username"
          label="操作人"
        />
        <el-table-column
          prop="begin"
          label="任务开始时间"
        />
        <el-table-column
          prop="finish"
          label="任务完成时间"
        />
        <el-table-column
          prop="filename"
          label="文件名"
        />
        <el-table-column
          label="文件大小"
        >
          <template slot-scope="{row}">
            <span>{{formatSizeHuman(row.fileSize)}}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          width="200"
          fixed="right"
        >
          <template slot-scope="{row}">
            <el-button type="text" size="small" icon="el-icon-see" @click="readLog(row.relativePath)">查看</el-button>
            <el-button type="text" size="small" icon="el-icon-download" @click="downloadLog(row.relativePath)">下载</el-button>
          </template>
        </el-table-column>

      </el-table>

      <!--分页组件-->
      <el-pagination
        :current-page="pageParam.currentPage"
        :page-size="pageParam.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="list.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-row>

    <!-- 查看执行日志 -->
    <el-dialog fullscreen :visible.sync="dialog.visible" :title="dialog.title" @close="cascadeMavenLog">
      <maven-exec-goal-log :file-path="dialog.filePath" ref="mavenLog"/>
    </el-dialog>
  </div>
</template>

<script>

import maven from '@/api/maven'
import core from '@/api/core'

import mavenExecGoalLog from '@/views/call/maven/mavenExecGoalLog'

import {formatSizeHuman} from '@/utils'

export default {
  name: 'mavenExecGoalList',
  components: {mavenExecGoalLog},
  data(){
    return {
      list: {data:[],total: 0},
      pageParam: {
        pageNo: 1,
        pageSize: 10
      },
      dialog: {
        visible: false,
        title: '执行日志',
        filePath: null
      }
    }
  },
  mounted() {
    this.logsByPage()
  },
  methods: {
    formatSizeHuman,
    logsByPage(){
      maven.executeLogManager.listPage(this.pageParam).then(res => {
        this.list = res.data
      })
    },
    readLog(logPath){
      this.dialog.filePath = logPath
      if (this.dialog.filePath){
        this.dialog.visible = true

        this.$nextTick(() => {
          this.$refs.mavenLog.init()
        })
      }

    },
    cascadeMavenLog(){
      this.$nextTick(() => {
        this.$refs.mavenLog.stopInterval()
      })
    },
    downloadLog(logPath){
      maven.executeLogManager.downloadLog(logPath).then(res => {
        core.fileDownload(res.data)
      })
    },
    handleCurrentChange(val) {
      this.pageParam.pageNo = val
      this.logsByPage()
    },
    handleSizeChange(val) {
      this.pageParam.pageSize = val
      this.logsByPage()
    }
  }
}
</script>

<style scoped>

</style>
